<?php

$app = isset($_GET['dir'])?$_GET['dir']:'coach';
$path = dirname(__FILE__).'/app/'.$app.'/controllers';
$baseurl = dirname('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$url = $baseurl."/{$app}.php/";

$list = init($path);
$count = 0;
foreach($list as $val){
	$count += count($val['list']);
}


$act = isset($_GET['act'])?$_GET['act']:'';
if($act=='export'){
	
	include('import.html');
	
	/*
	$word = new words(); 
	$word->start(); 
	$wordname = "export.doc"; 
	
	include('import.html');
	
	$word->save($wordname); 
	ob_flush();//每次执行前刷新缓存 
	flush(); 
	
	echo 'ok';
	exit;
	*/
	exit;
}

?>
<html>
<head>
	<meta charset="UTF-8">
	<title>Api文档</title>
	<link rel="stylesheet" type="text/css" href="./images/style.css"/>
	<script src="./images/main.js"></script>
</head>
<body style="background: url(http://pic1.sc.chinaz.coms/files/pic/pic9/201801/bpic5184.jpg) no-repeat;background-size: 100%;">

<div class="nav_box">
	<ul>
		<?php foreach($list as $key=>$row):?>
		<li><a href="#nav_<?php echo $key;?>">1.<?php echo ($key+1);?> <?php echo $row['name'];?>(<?php echo count($row['list']);?>)</a></li>
		<?php endforeach;?>
	</ul>
</div>

<div class="content">
<div class="line_box">
	<div class="line_box_short" >
		<span class="sline_tit">字段名称</span>
		<div class="sline_input">
			<input type="text" id="field_name" value="token" style="width: 80px;">
		</div>
	</div>
	<div class="line_box_short" >
		<span class="sline_tit">值</span>
		<div class="sline_input">
			<input type="text" id="token" value="Umg3dWJmcDdwYnVPYVpKd3JSZVM5Z3E0MWY2T0hjTWdOZ2NXaVlGMno=" style="width:300px;">
		</div>
	</div>
	<div class="line_box_short" >
		<span class="sline_tit">字段描述</span>
		<div class="sline_input" style="background: #eee;padding: 0 10px;">
			用户token
		</div>
	</div>
</div>


<div class="">
	一共有<span><?php echo $count;?></span>个接口
	<a href="?act=export">导出</a>
</div>



<?php 
	foreach($list as $key=>$row):
?>
<h1 id="nav_<?php echo $key;?>">1.<?php echo ($key+1);?> <?php echo $row['name'];?></h1>
<div class="box1" style="margin-left: 50px;">
<?php 
	foreach($row['list'] as $k=>$val):
	$id = '1.'.($key+1).'.'.($k+1);
?>
<h3>1.<?php echo ($key+1);?>.<?php echo ($k+1);?> <?php echo isset($val['name'])?$val['name']:'未编辑';?></h3>
<div>
	1.<?php echo ($key+1);?>方法名:  <?php echo $url.$val['func_name'];?>
</div>
<div>
	<p>1.2请求参数</p>
	<table border="1">
		<tr>
			<th>字段名称</th>
			<th>是否必填</th>
			<th>类型及范围</th>
			<th>说明</th>
			<th>自定义值</th>
		</tr>
		<?php if(isset($val['param'])):
			foreach($val['param'] as $params):
		?>
		<tr>
			<td><?php echo isset($params[0])?$params[0]:'';?></td>
			<td><?php echo isset($params[1])?$params[1]:'';?></td>
			<td><?php echo isset($params[2])?$params[2]:'';?></td>
			<td><?php echo isset($params[3])?$params[3]:'';?></td>
			<td><input type="text" class="val_<?php echo $id;?>"></td>
		</tr>
		<?php endforeach;?>
		<script>Wu.setData('<?php echo $id;?>','params','<?php echo json_encode($val['param']);?>');</script>
		<?php endif;?>
	</table>
</div>
<div>
	<p>请求方式：POST</p>
</div>
<div>
	<p>1.3返回结果</p>
	<div>
		<?php echo isset($val['return'])?$val['return']:'自己测试'?>
		<a href="javascript:;" onclick="Wu.run('<?php echo $id;?>','val_<?php echo $id;?>')">点击运行</a>
	</div>
	<script>
		Wu.setData('<?php echo $id;?>','url','<?php echo $url.$val['func_name'];?>');
		
		
	</script>
</div>
<hr>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>


<body>
</html>
<?php




function init($path,$dirname=''){


$garr = array();

	if($handle = opendir($path)){
		while (false !== ($file = readdir($handle))){

			if($file=='.' || $file=='..'){
				continue;
			}
			
			$ls_file = $path . '/'.$file;
			
			
			

			if(!file_exists($ls_file)){
				
				continue;
			}
			
			if(!is_dir($ls_file)){
				$ls_arr = get_data($ls_file,$dirname);
				if(!$ls_arr){
					continue;
				}
				
				$garr[] = array(
					'name'=>$ls_arr['name'],
					'list'=>$ls_arr['list']
				);
			}
			else{
				$dname = basename($ls_file);
				$arr = init($ls_file,$dname);
				
				$garr = array_merge($garr,$arr);

			}

		}
	}
	closedir($handle);
	return $garr;
}


function get_data($file,$dirname){
	
	$ww = file_get_contents($file);	
	$contr = '';
	preg_match("/class (.*?)[ |\{]/is",$ww,$matchs1);
	if(count($matchs1)>=2){
		$contr = $matchs1[1];
	}

	
	
	
	$name = '';
	preg_match("/\/\/(.*?)\nclass/is",$ww,$matchs1);
	if(count($matchs1)>=2){
		$name = $matchs1[1];
	}
	if(empty($name)){
		return false;
	}
	
	
	preg_match_all("/\/\/start(.*?)\/\/end.*?function(.*?)\(/is",$ww,$matchs);
	
	if(count($matchs)<=2){
		return false;
	}
	
	$arr = array(
		'name'=>'@name',
		'return'=>'@return',
		'param'=>' @param'
	);
	
	$list = array();
	foreach($matchs[1] as $index=>$val){
		
		$func_name = $matchs[2][$index];
		
		$fg = explode("\n",$val);
		
		$data = array();
		
		
		$ddd = !empty($dirname)?$dirname.'/':'';
		
		$data['func_name'] =  $ddd . $contr .'/'.trim($func_name);
		foreach($fg as $v){
			if(empty($v)  || strpos($v,'/*')){
				continue;
			}
			if(strpos($v,'*/')){
				break;
			}
			
			$ls_tt = trim($v);
			
			foreach($arr as $key=>$info){
				if(strpos($ls_tt,$info)){
					$ls_fg = explode($info,$ls_tt);
					if(count($ls_fg)>1){
						if($key=='param'){
							$params = trim($ls_fg[1]);
							
							$fg_p = explode(' ',$params);
							
							$data[$key][] = $fg_p;
						}
						else{
							$data[$key] = $ls_fg[1];
						}
						
					}
				}
			}
		}
		if(!isset($data['func_name'])){
			continue;
		}
		$list[] = $data;
	}
	
	$ls_arr = array(
		'name'=>$name,
		'list'=>$list
	);
	
	return $ls_arr;
}




class words{

	function start(){
		ob_start();
		echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"
		xmlns:w="urn:schemas-microsoft-com:office:word"
		xmlns="http://www.w3.org/TR/REC-html40">';
	}
	function save($path){
		  
		echo "</html>";
		$data = ob_get_contents();
		ob_end_clean();
		  
		$this->wirtefile ($path,$data);
	}
	  
	function wirtefile ($fn,$data){
		$fp=fopen($fn,"wb");
		fwrite($fp,$data);
		fclose($fp);
	}
}